import Mock from 'mockjs';
import { builder, getQueryParameters } from '../../util';

const total = 5701;

/* 列表数据 */
const page = (options: any) => {
  const parameters = getQueryParameters(options);
  const result = [];
  const current = parseInt(parameters.current) || 1;
  const pageSize = parseInt(parameters.pageSize) || 20;
  const totalPage = Math.ceil(total / pageSize);
  const next = (current >= totalPage ? total % pageSize : pageSize) + 1;

  for (let i = 1; i < next; i++) {
    result.push({
      id: Mock.mock('@id'),
      name: Mock.mock('@last'),
      type: ['x1', 'x2', 'x3', 'x4'][Mock.mock('@natural(0, 3)')],
      status: Mock.mock('@natural(0, 1)'),
      createTime: Mock.mock('@datetime'),
    });
  }

  return builder({
    data: {
      pageSize: pageSize,
      current: current,
      total: total,
      totalPage: totalPage,
      result,
    },
  });
};

/* 删除 */
const remove = (id: string) => {
  return builder({ data: true });
};

export default () => {
  Mock.mock(/\/api\/mock\/page\/list/, 'get', page);
  Mock.mock(/\/api\/mock\/page\/remove/, 'post', remove);
};
